package org.viva.action.cms;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.viva.api.ActionUtils;
import org.viva.cms.sitemap.BaiduSm;
import org.viva.core.api.Ajax;
import org.viva.core.util.UTIL;
import org.viva.entity.cms.Category;
import org.viva.service.cms.CategoryService;
import org.viva.service.sys.WebsiteCache;

@Controller
public class CategoryAction {

    @Resource
    private CategoryService categoryService;

    @RequestMapping(value = "/g/{code:[A-Za-z]+[0-9]*}", method = RequestMethod.GET)
    public String index(HttpServletRequest request, @PathVariable("code") String code, ModelMap modelMap) {
        Category category = categoryService.getByCode(code);
        return index(request, category.getId(), 1, modelMap);
    }

    @RequestMapping(value = "/g/{id:\\d+}", method = RequestMethod.GET)
    public String index(HttpServletRequest request, @PathVariable("id") Long id, ModelMap modelMap) {
        return index(request, id, 1, modelMap);
    }

    @RequestMapping(value = { "/g/{code:[A-Za-z]+[0-9]*}_{page:\\d+}" }, method = RequestMethod.GET)
    public String index(HttpServletRequest request, @PathVariable("code") String code, @PathVariable("page") Integer page, ModelMap modelMap) {
        Category category = categoryService.getByCode(code);
        return index(request, category.getId(), page, modelMap);
    }

    @RequestMapping(value = { "/g/{id:\\d+}_{page:\\d+}" }, method = RequestMethod.GET)
    public String index(HttpServletRequest request, @PathVariable("id") Long id, @PathVariable("page") Integer page, ModelMap modelMap) {
        Category category = categoryService.get(id);
        String cms_temppath = WebsiteCache.get("cms_temppath");
        modelMap.put("param", ActionUtils._getParameters());
        modelMap.put("ws", WebsiteCache.getConfig());
        modelMap.put("category", category);
        modelMap.put("pagecount", page);
        String temppath = category.getCgtp();
        if (StringUtils.isEmpty(temppath)) {
            temppath = "category";
        }
        return cms_temppath + "/" + temppath;
    }

    @RequestMapping(value = "/cms/category")
    public String adminPage(ModelMap modelMap) {
        modelMap.put("ws", WebsiteCache.getConfig());
        Map<String, Object> param = ActionUtils._getParameters();
        modelMap.put("param", param);
        modelMap.put("list", categoryService.getSelects());
        return "cms/category_page";
    }

    @RequestMapping(value = "/cms/category/edit", method = RequestMethod.GET)
    public String edit(ModelMap modelMap, Long id) {
        modelMap.put("ws", WebsiteCache.getConfig());
        Map<String, Object> param = ActionUtils._getParameters();
        modelMap.put("param", param);
        modelMap.put("cglist", categoryService.getRoot());
        modelMap.put("sysDate", UTIL.FORMAT.date(new Date(), "yyyy-MM-dd"));
        if (id != null) {
            Category category = categoryService.get(id);
            modelMap.put("data", category);
        }
        return "cms/category_edit";
    }

    @ResponseBody
    @RequestMapping(value = "/cms/category/save", method = RequestMethod.POST)
    public Map<String, Object> doSave(Category category) {
        if (category.getId() == null) {
            categoryService.add(category);
        } else {
            categoryService.up(category);
        }
        try {
            BaiduSm.cg(category.getCode() + "");
        } catch (Exception e) {
        }
        return Ajax.exec(category.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/cms/category/del")
    public Map<String, Object> doDel(Long id) {
        categoryService.del(id);
        return Ajax.exec();
    }
}
